package mop.security.acl;

/**
 * ACL Rule
 * @author MOP Team 7
 */
public class Rule {
	
	/**
	 * Elements a rule consists of
	 */
	private final Assertion assertion;
	private final Role role;
	private final Asset asset;
	private final String[] privileges;
	private final Type type;
	
	/**
	 * @param role			Role 		| null = all roles
	 * @param asset			Asset 		| null = all assets
	 * @param privilege		Privilege	| null = all privileges 
	 * @param assertion		Assertion
	 * @param type			Type
	 * 
	 * @post	Role is set to given role
	 * @post	Asset is set to given asset
	 * @post	Privileges are set to given privileges
	 * @post	Assertion is set to given assertion, or if none given, NoAssertion is set
	 * @post	Type is set
	 */
	Rule(Role role, Asset asset, String[] privileges, Assertion assertion, Type type){
		this.role = role;
		this.type = type;
		this.asset = asset;
		this.assertion = (assertion == null)? new NoAssertion() : assertion;
		this.privileges = privileges;
	}

	/**
	 * Get the assertion for this rule
	 * @return	assertion
	 */
	public Assertion getAssertion() {
		return assertion;
	}

	/**
	 * Get the role for this rule
	 * @return role
	 */
	public RoleInterface getRole() {
		return role;
	}

	/**
	 * Get the asset for this rule
	 * @return asset
	 */
	public AssetInterface getAsset() {
		return asset;
	}

	/**
	 * Get the privileges for this rule
	 * @return	privileges
	 */
	public String[] getPrivileges() {
		return privileges;
	}

	/**
	 * Get the type for this rule
	 * @return	type
	 */
	public Type getType() {
		return type;
	}
	
	/**
	 * Check whether this rule applies for given role
	 */
	public boolean definesRole(RoleInterface role){
		return getRole() == null || getRole().equals(role);
	}
	
	/**
	 * Check whether this rule applies for given asset
	 */
	public boolean definesAsset(AssetInterface asset){
		return getAsset() == null || getAsset().equals(asset);
	}
	
	/**
	 * Check whether this rule applies for given privilege
	 * @param privilege
	 * @return	boolean
	 */
	public boolean definesPrivilege(String privilege){
		if(privileges == null || privileges.length == 0)
			return true;
		for(String priv : privileges){
			if(priv.equals(privilege))
				return true;
		}
		return false;
	}
	
	
	
}
